package com.pig.controller.goods;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pig.exception.ServiceException;
import com.pig.pojo.goods.Comment;
import com.pig.pojo.goods.Goods;
import com.pig.pojo.user.User;
import com.pig.service.goods.CommentService;
import com.pig.service.goods.GoodsService;
import com.pig.util.PageObject;
import com.pig.util.SysResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/goods/")
public class GoodsControllerInWeb {
    @Reference(check = false)
    private GoodsService goodsService;

    @Reference(check = false)
    private CommentService commentService;

    /**
     * 查找所有的商品信息
     *
     * @param title
     * @param pageCurrent
     * @param categoryId
     * @param status
     * @param pageSize
     * @return
     */
    @GetMapping("/doFindAllGoods")
    @ResponseBody
    public SysResult doFindAllGoods(String title, Integer pageCurrent, Integer categoryId, Integer status, Integer pageSize) {
        PageObject<Goods> list = goodsService.findGoodsByTitle(title, status, categoryId, pageCurrent, pageSize);
        return SysResult.success(list);
    }

    /**
     * 查找指定信息
     *
     * @param goodsId
     * @param model
     * @return
     */
    @GetMapping("/{goodsId}")
    public String doFindGoodById(@PathVariable Integer goodsId, Model model) {
        //查找商品的所有字段信息
        Map<String, Object> goodsMap = goodsService.findGoodsById(goodsId);
        String[] imgArr =((String) goodsMap.get("img")).split(",");
        //System.out.println("img:" + Arrays.toString(imgArr));
        Map<String,String> imgMap = new HashMap<>();
        for (int i = 0 ; i < imgArr.length ; i++) {
            imgMap.put("img" + i, imgArr[i]);
        }
        //查找对应商品的评论信息,默认查找第一页数据
        PageObject<Comment> comment = commentService.findCommentByGoodsId(goodsId, 1);
        SysResult commResult = SysResult.success(comment);
        //List<Comment> commList = comment.getRecords();
        model.addAttribute("map", goodsMap);
        model.addAttribute("comment", commResult);
        model.addAttribute("img", imgMap);
        return "item";
    }

    /**
     * 指定商品的翻页查询
     * @param goodsId
     * @param pageCurrent
     * @return
     */
    @GetMapping("/doFindCommentByPageCurrent")
    @ResponseBody
    public SysResult doFindCommentByPageCurrent(Integer goodsId, Integer pageCurrent) {
        return SysResult.success();
    }

    @PostMapping("/doSaveComment")
    @ResponseBody
    public SysResult doSaveComment(Comment comment) {
        System.out.println(comment);
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user == null) {
            return SysResult.fail(new ServiceException("请登陆后再进行评论！"));
        }
        String username = user.getUsername();
        comment.setUsername(username);
        commentService.insertComment(comment);
        return SysResult.success("增加评论成功！",null);
    }

    @RequestMapping("/doGetRandomGoods")
    @ResponseBody
    public SysResult doGetRandomGoods() {
        return SysResult.success(goodsService.getRandomGoods());
    }
}
